//
//  ReaderConfig.m
//  ebook
//
//  Created by 吴超飞 on 16/3/13.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "ReaderConfig.h"
#import "UIImage+RTTint.h"
#import "FontModel.h"

@interface ReaderConfig()

@property (nonatomic,retain)UIColor *yangpizhiColor,*yanhuiColor,*huyanlvColor,*shushiColor,*menghuanColor;

@end

@implementation ReaderConfig

-(void)encodeWithCoder:(NSCoder *)aCoder{
    
    [aCoder encodeFloat:self.FontSize forKey:@"FontSize"];
    [aCoder encodeInteger:self.LineSpace forKey:@"LineSpace"];
    [aCoder encodeInteger:self.BgColorType forKey:@"BgColorType"];
    [aCoder encodeBool:self.IsNight forKey:@"IsNight"];
    [aCoder encodeInteger:self.PageTurnStyle forKey:@"PageTurnStyle"];
    [aCoder encodeObject:self.fontFullname forKey:@"fontFullname"];
    [aCoder encodeInteger:self.BeforeBgColorType forKey:@"BeforeBgColorType"];
    [aCoder encodeBool:self.orientationIsLandscape forKey:@"orientationIsLandscape"];
    [aCoder encodeInteger:self.autoReadingSpeed forKey:@"autoReadingSpeed"];
    [aCoder encodeInteger:self.phoneticReadingSpeed forKey:@"phoneticReadingSpeed"];
    //    [aCoder encodeInteger:self.DownloadType forKey:@"DownloadType"];
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    self = [super init];
    if (self) {
        _LineSpace = (LineSpace)[aDecoder decodeIntegerForKey:@"LineSpace"];
        _FontSize = [aDecoder decodeFloatForKey:@"FontSize"];
        _BgColorType = (ReadBackgroud)[aDecoder decodeIntegerForKey:@"BgColorType"];
        _IsNight = [aDecoder decodeBoolForKey:@"IsNight"];
        _PageTurnStyle = (PageTurnStyle)[aDecoder decodeIntegerForKey:@"PageTurnStyle"];
        _fontFullname = (NSString *)[aDecoder decodeObjectForKey:@"fontFullname"];
        _BeforeBgColorType = (ReadBackgroud)[aDecoder decodeIntegerForKey:@"BeforeBgColorType"];
        _orientationIsLandscape = [aDecoder decodeBoolForKey:@"orientationIsLandscape"];
        _autoReadingSpeed = [aDecoder decodeIntegerForKey:@"autoReadingSpeed"];
        _phoneticReadingSpeed = [aDecoder decodeIntegerForKey:@"phoneticReadingSpeed"];
        //        _DownloadType = (DownloadType)[aDecoder decodeIntegerForKey:@"DownloadType"];
    }
    return self;
}

-(void)setOrientationIsLandscape:(BOOL)orientationIsLandscape{
    _orientationIsLandscape = orientationIsLandscape;
}

#pragma mark - 边距设置
-(int)padingTop{
    return _padingTop = (int)MTConfigInteger([@"PagePadingTop" stringByAppendingString:self.orientationIsLandscape?@"_Landscape":@""]) + TopSpace;
}

-(int)padingBottom{
    return (int)MTConfigInteger([@"PagePadingBottom" stringByAppendingString:self.orientationIsLandscape?@"_Landscape":@""]) + BottomSpace;
}

-(int)padingRight{
    return _padingRight = (int)MTConfigInteger([@"PagePadingRight" stringByAppendingString:self.orientationIsLandscape?@"_Landscape":@""]) + RightSpace;
}

-(int)padingLeft{
    return _padingLeft = (int)MTConfigInteger([@"PagePadingLeft" stringByAppendingString:self.orientationIsLandscape?@"_Landscape":@""]) + LeftSpace;
}

-(int)padingTop_Updown{
    return _padingTop_Updown = (int)MTConfigInteger([@"PagePadingTop_Updown" stringByAppendingString:self.orientationIsLandscape?@"_Landscape":@""]);
}

-(int)padingBottom_Updown{
    return _padingBottom_Updown = (int)MTConfigInteger([@"PagePadingBottom_Updown" stringByAppendingString:self.orientationIsLandscape?@"_Landscape":@""]);
}

#pragma mark - 夜间设置
-(void)setIsNight:(BOOL)IsNight{
    
    _IsNight = IsNight;
    
    if (_IsNight && self.BgColorType != Night_Color) {
        _BeforeBgColorType = self.BgColorType;
    }
    
    self.BgColorType = _IsNight? Night_Color:_BeforeBgColorType;
}

#pragma  mark -颜色字体
-(void)setBgColorType:(ReadBackgroud)BgColorType{
    
    _BgColorType = BgColorType;
    _simuBackgroudImage = nil;//切换背景色后需要重新生成背景图片
    _batteryImage = nil;
}

//判断颜色值是否相等，因为double数据精度的问题，只比较四位相等即判断为同一颜色
-(BOOL)isColor:(UIColor *)color1 equalColor:(UIColor *)color2{
    
    CGFloat red1,green1,blue1,alpha1,red2,green2,blue2,alpha2;
    [color1 getRed:&red1 green:&green1 blue:&blue1 alpha:&alpha1];
    [color2 getRed:&red2 green:&green2 blue:&blue2 alpha:&alpha2];
    
    return [[NSString stringWithFormat:@"%0.4f%0.4f%0.4f%0.2f",red1,green1,blue1,alpha1] isEqualToString:[NSString stringWithFormat:@"%0.4f%0.4f%0.4f%0.2f",red2,green2,blue2,alpha2]];
}

-(void)setLineSpace:(LineSpace)LineSpace{
    _LineSpace = LineSpace;
}

-(void)setFontSize:(CGFloat)FontSize{
    _FontSize = FontSize;
}

- (NSString *)postScript {
    
    if (!_postScript) {
        _postScript = [self getpostscriptName];
    }
    return _postScript;
}

- (NSString *)getpostscriptName {
    
    __block NSString *postscript;
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:kFontsData];
    if(data.length!=0){
        NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        NSMutableArray *fontArray = [NSMutableArray array];
        for (NSDictionary *dic in array) {
            FontModel *model = [[FontModel alloc] initWithDictionary:dic];
            [fontArray addObject:model];
        }
        [fontArray enumerateObjectsUsingBlock:^(FontModel *model, NSUInteger idx, BOOL * stop) {
            if ([_fontFullname isEqualToString:model.fullname]) {
                postscript = model.postscript;
                *stop = YES;
            }
        }];
    }
    return postscript;
}

- (NSString *)fontName {
    
    if (!_fontName) {
        _fontName = [self getFontName];
    }
    return _fontName;
}

- (NSString *)getFontName {
    
    __block NSString *fontname;
    NSArray *array = nil;
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:kFontsData];
    if(data.length!=0){
        array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    }else{
        array = [NSArray arrayWithArray:[[AppConfigManager shareManager] FontsData]];
    }
    
    NSMutableArray *fontArray = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        FontModel *model = [[FontModel alloc] initWithDictionary:dic];
        [fontArray addObject:model];
    }
    [fontArray enumerateObjectsUsingBlock:^(FontModel *model, NSUInteger idx, BOOL * stop) {
        if (_fontName) {
            if ([_fontName isEqualToString:model.fontname]) {
                fontname = model.fontname;
                *stop = YES;
            }
        } else {//内存中不存在，根据fullName取值
            if ([_fontFullname isEqualToString:model.fullname]) {
                fontname = model.fontname;
                *stop = YES;
            }
        }
    }];
    return fontname;
}

#pragma mark - PageTurnStyle

-(UIPageViewControllerTransitionStyle)PageTransitionStyle{
    
    switch (_PageTurnStyle) {
            
        case PageTurnStyle_Simulat:
            _PageTransitionStyle = UIPageViewControllerTransitionStylePageCurl;
            break;
            
        case PageTurnStyle_Interlink:
        case PageTurnStyle_Updown:
            _PageTransitionStyle = UIPageViewControllerTransitionStyleScroll;
            break;
            
        default:
            _PageTransitionStyle = UIPageViewControllerTransitionStylePageCurl;
            break;
    }
    
    return _PageTransitionStyle;
}

-(UIPageViewControllerNavigationOrientation)PageNavigationOrientation{
    
    switch (_PageTurnStyle) {
            
        case PageTurnStyle_Simulat:
        case PageTurnStyle_Interlink:
            _PageNavigationOrientation = UIPageViewControllerNavigationOrientationHorizontal;
            break;
            
        case PageTurnStyle_Updown:
            _PageNavigationOrientation =  UIPageViewControllerNavigationOrientationVertical;
            break;
            
        default:
            _PageNavigationOrientation =  UIPageViewControllerNavigationOrientationHorizontal;
            break;
    }
    
    return _PageNavigationOrientation;
}

-(BOOL)IsPageTransitionNoAnimate{
    
    _IsPageTransitionNoAnimate = _PageTurnStyle==PageTurnStyle_NoAni;
    return _IsPageTransitionNoAnimate;
}

-(UIImage *)simuBackgroudImage{
    
    if(!_simuBackgroudImage){
        _simuBackgroudImage = [self imageFromColor:_ViewBgColor];
    }
    return _simuBackgroudImage;
}

//根据颜色生成全屏纯色图片
- (UIImage *)imageFromColor:(UIColor *)color{
    
    CGRect rect = CGRectMake(0, 0, MTScreenW, MTScreenH);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();
    
    return img;
}

#pragma mark - Color

-(UIColor *)ViewBgColor{
    
    NSArray *bgColor = @[self.yangpizhiColor,self.yanhuiColor,self.huyanlvColor,self.shushiColor,self.menghuanColor,PageNightBgColor];
    _ViewBgColor = bgColor[_BgColorType];
    
    return _ViewBgColor;
}

-(UIColor *)TitleTextColor{
    
    NSArray *titleTextColor=@[MTColor(147, 129, 105, 1.0),MTColor(142, 142, 142, 1.0),MTColor(135, 147, 134, 1.0),MTColor(128, 140, 149, 1.0),MTColor(145, 125, 131, 1.0),MTColor(80, 80, 80, 1.0)];
    _TitleTextColor = titleTextColor[_BgColorType];
    
    return _TitleTextColor;
}

-(UIColor *)ContentTextColor{
    
    NSArray *contentTextColor=@[MTColor(47, 41, 33, 1.0),MTColor(32, 32, 32, 1.0),MTColor(61, 81, 60, 1.0),MTColor(62, 77, 89, 1.0),MTColor(86, 61, 68, 1.0),MTColor(91, 91, 91, 1.0)];
    _ContentTextColor = contentTextColor[_BgColorType];
    
    return _ContentTextColor;
}

-(UIImage *)batteryImage{
    if (!_batteryImage) {
        _batteryImage = [[UIImage imageNamed:@"battery"] rt_tintedImageWithColor:self.TitleTextColor];
    }
    return _batteryImage;
}


#pragma mark - Color
-(UIColor *)shushiColor{
    if (!_shushiColor) {
        _shushiColor = [self colorWithImage:@"bg_shushi"];
    }
    return _shushiColor;
}

-(UIColor *)yangpizhiColor{
    if (!_yangpizhiColor) {
        _yangpizhiColor = [self colorWithImage:@"bg_yangpizhi"];
    }
    return _yangpizhiColor;
}

-(UIColor *)yanhuiColor{
    if (!_yanhuiColor) {
        _yanhuiColor = [self colorWithImage:@"bg_yanhui"];
    }
    return _yanhuiColor;
}

-(UIColor *)huyanlvColor{
    if (!_huyanlvColor) {
        _huyanlvColor = [self colorWithImage:@"bg_huyanlv"];
    }
    return _huyanlvColor;
}

-(UIColor *)menghuanColor{
    if (!_menghuanColor) {
        _menghuanColor = [self colorWithImage:@"bg_menghuan"];
    }
    return _menghuanColor;
}

//根据图片生成颜色
-(UIColor *)colorWithImage:(NSString *)imgName{
    
    UIImage *image = [UIImage imageNamed:imgName];
    return [UIColor colorWithPatternImage:image];
}

@end
